<?php
/**
 * 从给定的数据表中，把数据导入库里
 */
apf_require_class('Oa_Job_Abstract');
apf_require_class("Const_BaseInfo");
class Oa_Job_Hr_CheckUserData extends Oa_Job_Abstract {

    private $read_db = "hroa_slave";//读数据库
    private $writedb = "hroa_master";//写数据库

    public function run(){

        $file = dirname(__FILE__).'/data/checkdata.csv';

        if (!file_exists($file)) {
            $this->log($file."不存在,请输入正确的问题文件");exit;
        }
        $error = array();
        $this->file = fopen($file,"r");
        $time = time();
        while($temp = fgetcsv($this->file)){
            //print_r($temp);exit;
            $sql = "SELECT * FROM user where user_code='".trim($temp[3])."'";
            $user_data= $this->get_db_row($sql,$this->writedb,array());
            if(empty($user_data)){
                file_put_contents('/tmp/whatjj.txt',$temp[3]."Not Found\n",FILE_APPEND);
            }
            $user_name=trim($temp[4]);
            if($user_data['user_name']!=$user_name){
                file_put_contents('/tmp/whatjj.txt',$temp[3].$temp[4]."==".$user_data['user_name']."==".$user_name."=user_name\n",FILE_APPEND);
            }
            $join_start = $this->_build_data($temp[14]);
            $join_start = strtotime($join_start);
            $join_start = date('Ymd',$join_start);
            if($user_data['join_start']!=$join_start){
                file_put_contents('/tmp/whatjj.txt',$temp[3].$temp[4]."=".$user_data['join_start']."==".$join_start."==join_start\n",FILE_APPEND);
            }
            $join_end = $this->_build_data($temp[15]);
            $join_end = strtotime($join_end);
            $join_end = date('Ymd',$join_end);
            if($user_data['join_end']!=$join_end){
                file_put_contents('/tmp/whatjj.txt',$temp[3].$temp[4]."=".$user_data['join_end']."==".$join_end."==join_end\n",FILE_APPEND);
            }
            $indue_date = $this->_build_data($temp[16]);
            $indue_date = strtotime($indue_date);
            $indue_date = date('Ymd',$indue_date);
            if($user_data['indue_date']!=$indue_date){
                file_put_contents('/tmp/whatjj.txt',$temp[3].$temp[4]."==".$user_data['indue_date']."==".$indue_date."=indue_date\n",FILE_APPEND);
            }
            $sql2 = "update user set join_num = ".$temp[17]." where user_id=".$user_data['user_id'];
            $this->execute( $sql2,array(), $this->writedb);
        }
    }

    private function _build_data($string){
        $string = str_replace('／','/',$string);
        $string = str_replace('－','-',$string);
        return $string;
    }
}